package com.ai.controller.upload;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import scala.collection.mutable.StringBuilder;

import com.ai.common.controller.BaseController;
import com.ai.common.model.Result;
import com.ai.common.model.pan.SizeVO;
import com.ai.model.edu.user.PicResponse;
import com.ai.service.user.PicRoomService;

/**
 * 
 * Class Name : WebSiteCategoryController<br>
 * 
 * Description : 图片空间<br>
 * 
 */
@Controller
public class PicRoomController extends BaseController {

	private Logger log = Logger.getLogger(this.getClass());

	@Resource
	public PicRoomService picRoomService;
	@Value("${staticDomain}")
	private String staticDomain;
	@Value("${mainDomain}")
	private String  mainDomain;

	/**
	 * Description ： 资讯图片上传
	 * 
	 * @author Administrator 2015年6月17日 上午11:25:21
	 * @param myfile
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 *
	 */
	@RequestMapping("/picroom/uploadSinglePic")
	@ResponseBody
	public Result uploadSinglePic(MultipartFile myfile,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Result rs;
		Long operatorId = this.getBackendOperator(request).getId();
		if (log.isDebugEnabled()) {
			// log记录
			log.debug("收到用户[" + operatorId + "]的文件上传请求");
		}
		// 上传到图片服务器
		PicResponse picResponse = null;
		if (myfile.isEmpty()) {
			rs = new Result(false, Result.INPUT, "上传文件为空");
		} else {
			try {
				picResponse = picRoomService.handlSinglePicForProduct(myfile);
				if (picResponse == null || picResponse.getErrorMsg() != null) {
					rs = new Result(false, Result.INPUT, picResponse
							.getErrorMsg().toString());
				} else {
					rs = new Result(true, Result.SUCCESS, "");
//					picResponse.setUrl(request.getScheme() + "://"
//							+ InetAddress.getLocalHost().getHostAddress() + ":"
//							+ request.getLocalPort() + "/upload/images/"
//							+ picResponse.getUrl());
					picResponse.setUrl(mainDomain + "/upload/images/" + picResponse.getUrl());
					rs.addAttribute("picResponse", picResponse);
				}
			} catch (Exception e) {
				rs = new Result(false, Result.INPUT, "服务器出错上传失败");
				log.error("图片空间文件上传失败 " + e.getMessage());
			}
		}
		return rs;
	}

	@RequestMapping("/picroom/uploadCoverPic")
	public String uploadCoverPic(MultipartFile myfile, Model model)
			throws Exception {
		// 上传到图片服务器
		try {
			List<SizeVO> sizeVos = new ArrayList<SizeVO>();
			sizeVos.add(new SizeVO(100, 100));
			sizeVos.add(new SizeVO(200, 136));
			Result rs = picRoomService.uploadIdPhoto(myfile,
					"^.*?\\.(jpg|jpeg|png|gif|bmp)$", 2 * (2 << 19), false,
					sizeVos);
			model.addAttribute("result", rs);
		} catch (Exception e) {
			log.error("图片空间文件上传失败 " + e.getMessage());
		}
		return "/acquisition/uploadPicSuccess";
	}

	@RequestMapping("/picroom/uploadPic")
	@ResponseBody
	public String uploadPic(MultipartFile myfile, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String result = "";

		Result rs;
		Long operatorId = this.getBackendOperator(request).getId();
		// log记录
		log.error("收到用户[" + operatorId + "]的文件上传请求");
		// 上传到图片服务器
		try {
			rs = picRoomService.uploadIdPhoto(myfile,
					"^.*?\\.(jpg|jpeg|png|gif|bmp)$", 2 * (2 << 19), false,
					null);
			String name = myfile.getOriginalFilename();
			myfile.getSize();
			if (rs != null && rs.isSuccess()) {
				String url = new StringBuilder(staticDomain).append(
						rs.getAttribute("url")).toString();
				result = "{\"name\": " + name + ", \"url\": " + url
						+ ",\"size\": " + myfile.getSize() + "}";
			}
		} catch (Exception e) {
			log.error("图片空间文件上传失败 " + e.getMessage());
		}
		return result;
	}
}
